package com.playball.lottery.frm.yhzx;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;


/**
 * 安全中心 - 提款密码
 * 
 * @author tanglizhong
 * @version 2014-3-26 上午10:37
 */
public class ScWithdrawPwdActivity extends BaseActivity {

	/** 标题栏 */
	private TextView info;

	/**
	 * 设置提款密码布局
	 */
	private View view_sc_withdraw_pwd;
	/** 提款密码 */
	private EditText yhzx_sc_et_withdraw_password;
	/** 确认密码 */
	private EditText yhzx_sc_et_withdraw_password_confirm;
	/** 设置按钮 */
	private Button yhzx_sc_et_withdraw_password_submit;

	/**
	 * 回答问题布局
	 */
	private View view_sc_withdraw_question;
	/** 安全问题 */
	private TextView yhzx_sc_tv_question;
	/** 安全答案 */
	private EditText yhzx_sc_et_answer;
	/** 下一步按钮 */
	private Button yhzx_sc_btn_next;

	/**
	 * 修改提款密码布局
	 */
	private View view_sc_withdraw_change;
	/** 原提款密码 */
	private EditText yhzx_sc_et_withdraw_password_old;
	/** 新提款密码 */
	private EditText yhzx_sc_et_withdraw_password_new;
	/** 确认提款密码 */
	private EditText yhzx_sc_et_withdraw_password_new_confirm;
	/** 修改按钮 */
	private Button yhzx_sc_et_withdraw_submit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yhzx_sc_withdraw_pwd);

		initView();
		titleBar();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		info.setText("设置提款密码");
		yhzx_sc_tv_question.setText(ApplicationData.getInstance().getSecurityQuestion());
		if (ApplicationData.getInstance().getIsPayPwd() == 0) {
			view_sc_withdraw_pwd.setVisibility(View.VISIBLE);
			view_sc_withdraw_question.setVisibility(View.GONE);
			view_sc_withdraw_change.setVisibility(View.GONE);
		} else {
			view_sc_withdraw_pwd.setVisibility(View.GONE);
			// 暂时屏蔽安全问题
			// if("".equals(ApplicationData.getInstance().getSecurityQuestion()))
			// {
			// view_sc_withdraw_question.setVisibility(View.GONE);
			// view_sc_withdraw_change.setVisibility(View.VISIBLE);
			// } else {
			// view_sc_withdraw_question.setVisibility(View.VISIBLE);
			// view_sc_withdraw_change.setVisibility(View.GONE);
			// }
			view_sc_withdraw_question.setVisibility(View.GONE);
			view_sc_withdraw_change.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		info = (TextView) findViewById(R.id.top_title_info);

		view_sc_withdraw_pwd = findViewById(R.id.view_sc_withdraw_pwd);
		yhzx_sc_et_withdraw_password = (EditText) findViewById(R.id.yhzx_sc_et_withdraw_password);
		yhzx_sc_et_withdraw_password_confirm = (EditText) findViewById(R.id.yhzx_sc_et_withdraw_password_confirm);
		yhzx_sc_et_withdraw_password_submit = (Button) findViewById(R.id.yhzx_sc_et_withdraw_password_submit);
		yhzx_sc_et_withdraw_password_submit.setOnClickListener(btn_onclick);

		view_sc_withdraw_question = findViewById(R.id.view_sc_withdraw_question);
		yhzx_sc_tv_question = (TextView) findViewById(R.id.yhzx_sc_tv_question);
		yhzx_sc_et_answer = (EditText) findViewById(R.id.yhzx_sc_et_answer);
		yhzx_sc_btn_next = (Button) findViewById(R.id.yhzx_sc_btn_next);
		yhzx_sc_btn_next.setOnClickListener(btn_onclick);

		view_sc_withdraw_change = findViewById(R.id.view_sc_withdraw_change);
		yhzx_sc_et_withdraw_password_old = (EditText) findViewById(R.id.yhzx_sc_et_withdraw_password_old);
		yhzx_sc_et_withdraw_password_new = (EditText) findViewById(R.id.yhzx_sc_et_withdraw_password_new);
		yhzx_sc_et_withdraw_password_new_confirm = (EditText) findViewById(R.id.yhzx_sc_et_withdraw_password_new_confirm);
		yhzx_sc_et_withdraw_submit = (Button) findViewById(R.id.yhzx_sc_et_withdraw_submit);
		yhzx_sc_et_withdraw_submit.setOnClickListener(btn_onclick);
	}

	/**
	 * 按钮点击事件
	 */
	private View.OnClickListener btn_onclick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			JSONObject jsob = new JSONObject();
			String[] params = new String[2];
			try {
				switch (v.getId()) {
				case R.id.yhzx_sc_et_withdraw_password_submit:
					// 新密码
					String pwd = yhzx_sc_et_withdraw_password.getText().toString();
					String pwd2 = yhzx_sc_et_withdraw_password_confirm.getText().toString();
					if (pwd.indexOf(" ") >= 0) {
						yhzx_sc_et_withdraw_password.setError("密码中不能使用空格");
						return;
					}
					if (pwd2.indexOf(" ") >= 0) {
						yhzx_sc_et_withdraw_password_confirm.setError("密码中不能使用空格");
						return;
					}
					if (pwd.length() < 6 || pwd.length() > 15) {
						yhzx_sc_et_withdraw_password.setError("请输入6到15位密码");
						return;
					} else if (!pwd.equals(pwd2)) {
						yhzx_sc_et_withdraw_password_confirm.setError("确认密码不正确");
						return;
					}
					params[0] = "130";
					jsob.put("oldPw", "");
					jsob.put("newPw", pwd);
					break;
				case R.id.yhzx_sc_btn_next:
					params[0] = "122";
					jsob.put("question", ApplicationData.getInstance().getSecurityQuestion());
					jsob.put("oldanswer", yhzx_sc_et_answer.getText().toString());
					break;
				case R.id.yhzx_sc_et_withdraw_submit:
					String oldpwd = yhzx_sc_et_withdraw_password_old.getText().toString();
					String pwdc = yhzx_sc_et_withdraw_password_new.getText().toString();
					String pwdc2 = yhzx_sc_et_withdraw_password_new_confirm.getText().toString();
					if (oldpwd.indexOf(" ") >= 0) {
						yhzx_sc_et_withdraw_password_old.setError("密码中不能使用空格");
						return;
					}
					if (pwdc.indexOf(" ") >= 0) {
						yhzx_sc_et_withdraw_password_new.setError("密码中不能使用空格");
						return;
					}
					if (pwdc2.indexOf(" ") >= 0) {
						yhzx_sc_et_withdraw_password_new_confirm.setError("密码中不能使用空格");
						return;
					}
					if (oldpwd.length() < 6 || oldpwd.length() > 15) {
						yhzx_sc_et_withdraw_password_old.setError("请输入6到15位密码");
					} else if (pwdc.length() < 6 || pwdc.length() > 15) {
						yhzx_sc_et_withdraw_password_new.setError("请输入6到15位密码");
						return;
					} else if (!pwdc.equals(pwdc2)) {
						yhzx_sc_et_withdraw_password_new_confirm.setError("确认密码不正确");
						return;
					}
					params[0] = "131";
					jsob.put("oldPw", oldpwd);
					jsob.put("newPw", pwdc);
					break;
				}
			} catch (JSONException ex) {
				return;
			}
			WithdrawTask task = new WithdrawTask();
			params[1] = jsob.toString();
			task.execute(params);
		}
	};

	/** TitleBar标题栏 */
	public void titleBar() {
		info.setText(R.string.yhzx_sc_withdraw_pwd);
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	private class WithdrawTask extends AsyncTask<String, Integer, String> {
		ShowTipDialog showdialog = new ShowTipDialog();
		String action = "";

		@Override
		protected void onPreExecute() {
			showdialog.showDialog(ScWithdrawPwdActivity.this);
		}

		@Override
		protected String doInBackground(String... params) {
			action = params[0];
			String strResult = null;

			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params[1], UserID, action, getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			int code = -1;
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					code = js.getInt("Code");
					if (code == 0) {
						if (action.equals("122")) {
							view_sc_withdraw_pwd.setVisibility(View.GONE);
							view_sc_withdraw_question.setVisibility(View.GONE);
							view_sc_withdraw_change.setVisibility(View.VISIBLE);
						} else if (action.equals("130")) {
							ApplicationData.getInstance().setIsPayPwd(1);
						}
					}
					String tip = js.getString("Data");
					showTip(tip);
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			} finally {
				showdialog.closeDialog();
				if (code == 0 && (action.equals("130")||action.equals("131"))) {
					ScWithdrawPwdActivity.this.finish();
				}
			}
		}
	}

	/** 显示文字提示 */
	private void showTip(String tip) {
		Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
	}
}
